// pages/custom/settings/bind/bind.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    telphone: '',
    sms: '',
    disabled: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  bindTelphone: function() {
    let that = this

    if(that.data.sms == '') {
      wx.showToast({
        title: '请填写验证码',
      })

      return false
    }

    wx.showLoading({
      title: app.globalData.loadText,
      mask: true
    })

    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/customer/bind-mobile?access_token=' + accessToken,
        data: {
          mobile: that.data.telphone,
          code: that.data.sms
        },
        success: res => {
          if(res.data.status === 1) {
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: '绑定成功',
              success: res => {
                if(res.confirm){
                  wx.switchTab({
                    url: '/pages/user/user',
                  })
                }
              }
            })
          }
        }
      })
    })
  },

  getSms: function() {
    let that = this
    if(that.data.telphone == '') {
      wx.showToast({
        title: '请填写手机号码',
      })
      
      return false
    }

    that.setData({
      disabled: true
    })
    
    wx.showLoading({
      title: app.globalData.loadText,
      mask: true
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/customer/send-sms-bind?access_token=' + accessToken,
        data: {
          mobile: that.data.telphone
        },
        success: res => {
          wx.hideLoading()
          setTimeout(function () {
            that.setData({
              disabled: false
            })
          }, 60000)
        }
      })
    })
  },

  phoneValue: function(e) {
    this.setData({
      telphone: e.detail
    })
  },

  smsValue: function(e) {
    this.setData({
      sms: e.detail
    })
  }
})